﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Revis.VClinicNet.Core.Domain
{
    public class WorkTime : IEquatable<WorkTime>
    {
        public virtual long Id { get; set; }
        public virtual DayOfWeek Day { get; set; }
        public virtual Time BeginTime { get; set; }
        public virtual Time EndTime { get; set; }
        public virtual string Room { get; set; }
        public virtual bool IsWorking { get; set; }
        public virtual Doctor Doctor { get; set; }

        #region Constructors

        public WorkTime()
        {
            BeginTime = new Time();
            Day = DayOfWeek.Monday;
            EndTime = new Time();
            Room = String.Empty;
            IsWorking = true;
            Doctor = null;
        }

        public WorkTime(DayOfWeek day, int bh, int bm, int eh, int em, string room, bool isWorking) :
            this(day, new Time(bh, bm), new Time(eh, em), room, isWorking)
        {
        }

        public WorkTime(DayOfWeek day, int bh, int bm, int eh, int em, string room, bool isWorking, Doctor doctor) :
            this(day, new Time(bh, bm), new Time(eh, em), room, isWorking)
        {
            if (doctor == null) {
                throw new ArgumentNullException("Null doctor");
            }
            Doctor = doctor;
        }

        public WorkTime(DayOfWeek day, Time begin, Time end, string room, bool isWorking)
        {
            if (begin == null || end == null) {
                throw new ArgumentNullException("Null time");
            }
            if (begin.CompareTo(end) >= 0) {
                throw new ArgumentException("Niepoprawne godziny begin<end");
            }
            if (room == null) {
                throw new ArgumentNullException("Null room");
            }
            Day = day;
            BeginTime = begin;
            EndTime = end;
            Room = room;
            IsWorking = isWorking;
            Doctor = null;
        }

        public override int GetHashCode()
        {
            return Id.GetHashCode() ^ base.GetHashCode();
        }

        #endregion

        #region Public Methods

        public virtual void Change(Time begin, Time end, string room, bool isWorking)
        {
            if (begin == null || end == null) {
                throw new ArgumentNullException("Null time");
            }
            if (begin.CompareTo(end) >= 0) {
                throw new ArgumentException("Niepoprawne godziny begin<end");
            }
            if (room == null) {
                throw new ArgumentNullException("Null room");
            }
            BeginTime = begin;
            EndTime = end;
            Room = room;
            IsWorking = isWorking;
        }

        public virtual bool IsDateBetweenWorkTime(DateTime date)
        {
            return
                date.DayOfWeek == Day &&
                date >= BeginTime.DateTime(date) &&
                date <= EndTime.DateTime(date);
        }

        public override bool Equals(object obj)
        {
            return Equals(obj as WorkTime);
        }

        public virtual bool Equals(WorkTime other)
        {
            if (other == null) {
                return false;
            }
            return this.Id == other.Id;
        }

        #endregion
    }
}
